// pages/index/index.js
import api from '../../request/api.js';
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 轮播图数组
    image_url:'',
    swiperList: [],
	  active: 0,
	  icon: {
      normal: 'https://img.yzcdn.cn/vant/user-inactive.png',
      active: 'https://img.yzcdn.cn/vant/user-active.png',
	  },
    allCategory:[],
    show: false,
    agreements:{},
    oneImages:[], //底部文章图片
    twoImages:[], //底部文章图片
    agree : true
  },
	onChange(event) {
	    this.setData({ active: event.detail });
	  },
  async getAllCategory(){
    const { data: res } = await api('GET', 'Recycle/other/getAllCategory')
    if(res.code===200){
      this.setData({
        allCategory: res.data
      })
    }
  },
  goPlaceOrder(e){
    if(!app.globalData.agree){Toast('请确认肥收环保上门回收免责');return;}
    let id = e.target.id
    let category = e.currentTarget.dataset.category
    wx.setStorageSync('orderAddress', '')
    wx.setStorageSync('category', category)
    wx.navigateTo({
      url: '../PlaceOrder/index?id='+category.category_id,
    })
  },
  async getBannerImage(){
    const { data: res } = await api('GET','Recycle/other/getBannerImage')
    if(res.code===200){
      this.setData({
        swiperList: res.data
      })
    }
  },
  async getAgreement(){
    const { data: res } = await api('GET','Recycle/other/getClause')
    if(res.code===200){
      this.setData({
        agreements: res.data
      })
    }
  },
  lookAgreement(){
    this.setData({
      show: true
    })
  },
  onClose(){
    this.setData({
      show:false
    })
  },
  async getArticleImage(){
    const { data: res } = await api('GET', 'Recycle/other/getArticleImage')
    if(res.code===200){
      let one = []
      let two = []
      res.data.forEach(item => {
        if(item.size==1){
          one.push(item)
        }else{
          two.push(item)
        }
      })
      this.setData({
        oneImages: one,
        twoImages: two
      })
    }
  },
  //去图片详情
  goBanner(e) {
    const id = e.currentTarget.dataset.imgid
    wx.navigateTo({
      url: '../banner/index?id='+id,
    })
  },
  //去文章详情
  goArticle(e) {
    const id = e.target.id
    wx.navigateTo({
      url: '../article/index?id=' + id,
    })
  },

  checkboxChange(){
    let that = this;
    app.globalData.agree = !(that.data.agree);
    that.setData({
      agree : !(that.data.agree)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      image_url: app.globalData.imageUrl
    })
    this.getAllCategory()
    this.getBannerImage()
    this.getAgreement()
    this.getArticleImage()
  },
	
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      agree : app.globalData.agree
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})